//
//  TRCTSettingsViewController.swift
//  onemeetingmac
//
//  Created by 张衡 on 2020/2/26.
//  Copyright © 2020 onemeeting. All rights reserved.
//

import Foundation

class TRCTSettingsViewController: BaseViewController {
    
     var rootArray = [RootModel]()      // 根节点数组
    
    override func viewDidLoad() {
        super.viewDidLoad()
        setupDataModel()
        setupuUI()
    }
    
    func setupuUI() {
        self.view.addSubview(scrollView)
        scrollView.snp.makeConstraints { (make) in
            make.left.bottom.equalToSuperview()
            make.top.equalTo(20)
            make.width.equalTo(300)
        }
        outLineView.expandItem(nil, expandChildren: true)
        
    }
    
    lazy var scrollView: NSScrollView = {
        let view = NSScrollView.init(frame: NSRect.zero)
        view.hasVerticalScroller = true
        view.autohidesScrollers = true
        view.documentView = self.outLineView
        return view
    }()
    
    lazy var outLineView: NSOutlineView = {
        let view = NSOutlineView.init(frame: NSRect.zero)
        view.wantsLayer = true
        view.backgroundColor = color_gray(200)
        view.gridColor = color_gray(200)
        
        view.rowSizeStyle = .large
        //view.floatsGroupRows = false
        //view.focusRingType = NSFocusRingType.none
        view.selectionHighlightStyle = NSTableView.SelectionHighlightStyle.none
        
        view.usesAlternatingRowBackgroundColors = false
        //view.autosaveExpandedItems = true
        
        let column = NSTableColumn.init(identifier: NSUserInterfaceItemIdentifier.init("column"))
        view.addTableColumn(column)
        //column.width = self.view.frame.width
        view.headerView = nil
        
        view.dataSource  = self
        view.delegate = self
        return view
    }()
}

extension TRCTSettingsViewController:NSOutlineViewDelegate,NSOutlineViewDataSource {
    func outlineView(_ outlineView: NSOutlineView, numberOfChildrenOfItem item: Any?) -> Int {
        if let itme = item as? RootModel {
            return itme.children.count
        }
        return rootArray.count
    }
    
    func outlineView(_ outlineView: NSOutlineView, isItemExpandable item: Any) -> Bool {
        return item is RootModel
    }
    func outlineView(_ outlineView: NSOutlineView, child index: Int, ofItem item: Any?) -> Any {
        if let item = item as? RootModel {
            return item.children[index]
        }
        return rootArray[index]
    }
    
    func outlineView(_ outlineView: NSOutlineView, isGroupItem item: Any) -> Bool {
        return true
    }
    
    func outlineView(_ outlineView: NSOutlineView, heightOfRowByItem item: Any) -> CGFloat {
        return 44
    }
    func outlineViewSelectionDidChange(_ notification: Notification) {
        let outlineView = notification.object as? NSOutlineView
        let row = outlineView?.selectedRow
        let model = outlineView?.item(atRow: row!)
        if model is RootModel {
            let rootModel = (model as? RootModel)
            print("model=\(rootModel?.name)")
        }else {
            let LeafModel = (model as? LeafModel)
            print("model=\(LeafModel?.name)")
        }
        
    }
    func outlineView(_ outlineView: NSOutlineView, viewFor tableColumn: NSTableColumn?, item: Any) -> NSView? {
        var cell:SettingsTabCellView?
        if item is RootModel {
            cell = outlineView.makeView(withIdentifier: NSUserInterfaceItemIdentifier(rawValue: "HeaderCell"), owner: self) as? SettingsTabCellView
            if cell == nil {
                cell = SettingsTabCellView.init()
            }
            cell?.title.stringValue = (item as? RootModel)?.name ?? "3"
        }else {
            cell = outlineView.makeView(withIdentifier: NSUserInterfaceItemIdentifier(rawValue: "DataCell"), owner: self) as? SettingsTabCellView
            if cell == nil {
                cell = SettingsTabCellView.init()
            }
            cell?.title.stringValue = (item as? LeafModel)?.name ?? "222"
        }
        return cell
    }
    
}


extension TRCTSettingsViewController {
    fileprivate func setupDataModel(){
        let leaf1 = LeafModel("Objective - C")
        let leaf2 = LeafModel("Swift")
        let leaf3 = LeafModel("Java")
        let leaf4 = LeafModel("Python")
        let root1 = RootModel()
        root1.name = "Language"
        root1.children = [leaf1,leaf2,leaf3,leaf4]
        
        let leaf5 = LeafModel("Beijing")
        let leaf6 = LeafModel("Guangzhou")
        let leaf7 = LeafModel("Shanghai")
        let leaf8 = LeafModel("Shenzhen")
        let root2 = RootModel()
        root2.name = "City"
        root2.children = [leaf5,leaf6,leaf7,leaf8]
        
        rootArray = [root1,root2]
        
        self.outLineView.reloadData()
    }
}


